/**
 * 宽带预约列表
 */
(function($) {
  var pageData = {};
  var pageCount = 1;
  var filterType = 3;
  var pageNumber = 0;
  var bulletinList = new Array();
  /*
  封装参数
  */
  var getMyParams = function(id) {
    var params = "?";
    var myParam = getParams();
    if (myParam) {
      params += myParam;
      if (id) {
        params += "&";
      }
    }
    if (id) {
      params += "id=" + id;
    }

    return params;
  };

  var bind = function() {
    itemOnClick("#bulletin-list > li");
  }

  var itemOnClick = function(object) {
    $(object).off("click"); //取消li事件
    /*li事件*/
    $(object).on("click", function() {
      var $siblingsNode = $(this).siblings();
      var primaryClass = "select-item";
      var defaultColor = "#fff";
      var selectColor = "#0c79b1";
      var idVal = $(this).find("input[name='id']").val();
      var titleVal = $(this).find("div[name='title']").html();
      var content = $(this).find("input[name='content']").val();
      var createDate = $(this).find("div[name='create_date']").html();
      var creator = $(this).find("input[name='creator']").val();

      if ($siblingsNode.hasClass(primaryClass)) {
        $siblingsNode.removeClass(primaryClass);
        $siblingsNode.css("background-color", defaultColor);
      }
      if ($(this).hasClass(primaryClass)) {
        $(this).removeClass(primaryClass);
        $(this).css("background-color", defaultColor);
        $id.val("");
        $name.html("");
      } else {
        $(this).addClass(primaryClass);
        $(this).css("background-color", selectColor);
      }
      setCookie("bulletin_title", titleVal, 1);
      setCookie("bulletin_content", content, 1);
      setCookie("bulletin_date", createDate, 1);
      setCookie("bulletin_creator", creator, 1);
      window.location.href = "tasklog_detail.html" + getMyParams(idVal);
      event.stopPropagation();
    });
  }

  var showTaskLogList = function(list) {
    var unreadCount = 0;
    var readCount = 0;
    var dataList = [];

    console.log("filterType=" + filterType);
    for (var i = 0; i < list.length; i++) {
      switch (list[i].readState) {
        case "1":
          readCount++;
          console.log("readCount=" + readCount);
          break;
        case "2":
          unreadCount++;
          console.log("unreadCount=" + unreadCount);
          break;
      }
      if (list[i].readState == filterType || filterType == 3) {
        dataList.push(list[i]);
      }
    }

    $("#task_log_sumary").html("任务日志" + list.length + "条");

    var $bulletin_list = $('#amz-list');
    var source = $bulletin_list.text();
    var template = Handlebars.compile(source);
    var data = {
      bulletinList: dataList,
      "error": "无任务日志信息!"
    };
    $("#bulletin-list").children('li').remove();
    $("#bulletin-list").children('div').remove();
    var html = template(data);
    $bulletin_list.before(html);
    bind();
  }

  /*
  显示数据
  */
  var showDatas = function(data) {
    var $bulletin_list = $('#amz-list');
    var source = $bulletin_list.text();
    var template = Handlebars.compile(source);    
    var unreadCount = 0;
    var readCount = 0;

    if(data.rtnCode!=1000){
      alert(data.rtnMsg);
      return;
    }
    var list = data.return.workNoteInfos;
    if(list.length==0){
      pageNumber--;
      console.log("pageNumber="+pageNumber);
    }

    for (var i = 0; i < list.length; i++) {
      list[i].actionKind = getActionKind(list[i].actionKind.trim());
      console.log("list[i].actionKind=" + list[i].actionKind);
      if (list[i].notifyContent) {
        list[i].notifyContent = list[i].notifyContent.replace(/\r\n/ig, "<br/>");
      }
      bulletinList.push(list[i]);
    }
    showTaskLogList(bulletinList);
  };

  /*
  加载数据
  */
  var loadDatas = function(pageNumber) {
    var today = new Date();
    var lastMonthToday = new Date();
    lastMonthToday.setMonth(lastMonthToday.getMonth()-1);

    $('#modal').modal('open');
    var empCode = getParamValue("empCode");
    var data = JSON.stringify(getQueryJsonParams(getParamValue("token"), empCode, lastMonthToday.format("yyyy-MM-dd")+ " 00:00:00", today.format("yyyy-MM-dd")+" 23:59:59","", "", pageNumber, 30));
    console.log("data=" + data);
    $.ajax({
      type: "post",
      contentType: "application/json",
      url: Constants.rpcEsopHost+"queryWorkNotes",
      data: data,
      dataType: "json",
      headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
        $('#modal').modal('close');
        //$.AMUI.progress.done();
        pageData = data;
        console.info(data);
        showDatas(data);
      },
      error: function(textStatus) {
        $('#modal').modal('close');
        alert("获取任务日志失败！");
        console.info("request error:"+textStatus);
      }
    });
  };

  $(function() {
    /*滚动条*/
    var $doc = $(document);
    $doc.scroll(function() {
      console.log("height = " + ($doc.height() - $(window).height()));
      console.log("scrollTop = " + Math.ceil($doc.scrollTop()));
      console.log("Math.ceil($doc.scrollTop()) = " + Math.ceil($doc.scrollTop()));
      if (Math.ceil($doc.scrollTop()) >= ($doc.height() - $(window).height())) {
        var $msg = $("#no-data-msg");
        console.log("debug 1");
        if (!$.isEmptyObject(pageData)) {
          console.log("debug 2");
          if (!$msg.hasClass("am-hide")) {
            console.log("debug 3");
            $msg.addClass("am-hide");
          }
          console.log("pageNumber="+pageNumber);
          pageNumber ++;
          loadDatas(pageNumber);
        } else {
          console.log("debug 5");
          if ($msg.hasClass("am-hide")) {
            console.log("debug 6");
            $msg.removeClass("am-hide");
          }
        }
      }
    });   

    /*加载模态窗口*/
    var options = {
      closeViaDimmer: false
    };
    $('#modal').modal(options);    
    loadDatas(pageNumber);
  });
})(jQuery);